package com.yfp.common.core.utils;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

/**
 * @Description: 企业微信机器人发送
 *
 * @CreateDate: 2024/9/13 15:21
 * @UpdateUser: 
 * @UpdateDate: 2024/9/13 15:21
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@Slf4j
@Component
public class WeixinRobotSender {

    private static Properties properties;

//    static {
//        properties = new Properties();
//        InputStream inputStream = WeixinRobotSender.class.getClassLoader().getResourceAsStream("application.yml");
//        try {
//            properties.load(inputStream);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    public static String getConfigValue(String key) {
        return properties.getProperty(key);
    }

    private static final List<String> WECHAT_ROBOT_WEBHOOK = Arrays.asList(
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=bdbb6f59-38ef-4edb-9222-3f5eed33b945",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=0a1309bb-aef1-4cc9-a258-02cdf17e13cc",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=47cf05aa-c1e7-45bb-8921-6e830c7a1595",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=e677459f-b308-4fa3-aadb-1b47f5a34724",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=48e33491-71be-47b5-9521-9d720702cf55",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7376881e-e30a-4ba3-88b6-b8b549a606ca",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fdd2892d-b6d3-4028-95af-6641030cde97",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c57268cd-1660-4709-8261-bb234a2d1024",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=ff2cc7ec-2075-4123-bebf-87cb2f3dc218",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=fd97b6dc-e7b3-4d46-8d89-7293bf6f645f"
    );

    private static List<String> BUG_WEBHOOK = Arrays.asList(
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=a4479a7c-4fe9-46b8-8eea-7795497fd369",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=7ca4ecb4-39f7-4804-85cb-e2e065fddc04",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=6a2615bd-09b9-4945-8ebb-a2e93376ab19",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=5b220ba8-b9a5-45bf-a00e-dd651900e456",
//            "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=f2b37b52-c829-4d43-92ab-f7266e50b03c"
    );
    private static int index = 0;
    private static int bugIndex = 0;

    public static void sendTextMessage(String content, ApplicationContext applicationContext) {
//        String[] activeProfiles = applicationContext.getEnvironment().getActiveProfiles();
//        String activeProfile = activeProfiles[0];
//        log.info("activeProfiles:{}", activeProfile);
//        if (activeProfile.equals("prod")) {
//            Map<String, Object> params = new HashMap<>();
//            params.put("msgtype", "text");
//            Map<String, Object> text = new HashMap<>();
//            text.put("content", content);
//            params.put("text", text);
//
//            if (index > 9) {
//                index = 0;
//            }
//            String url = WECHAT_ROBOT_WEBHOOK.get(index);
//            String json = JSONUtil.toJsonStr(params);
//            String result = post(url, json);
//            index = index + 1;
//            log.info("企业微信Message sent, result: {}", result);
//        }
    }


    public static void main(String[] args) throws InterruptedException {
//
//        for (int i=0;i<100; i++) {
//            String content = "### 异常温度报警\n" +
//                    " - 设备编号： 24101502000018\n" +
//                    " - 设备名称： 人工智能产业园18号机\n";
//            content= content+  " - 报警时间： " + DateUtils.getTime()+
//                    " - 当前温度： 12°C\n";
//            sendTextMessage(content, null);
//            try {
//                Thread.sleep(10000L);
//            } catch (Exception e){
//                e.printStackTrace();
//            }
//        }

    }

    public static void sendBUGTextMessage(String content, ApplicationContext applicationContext) {
//        String[] activeProfiles = applicationContext.getEnvironment().getActiveProfiles();
//        String activeProfile = activeProfiles[0];
//        if (activeProfile.equals("prod")) {
//            Map<String, Object> params = new HashMap<>();
//            params.put("msgtype", "text");
//            Map<String, Object> text = new HashMap<>();
//            text.put("content", content);
//            params.put("text", text);
//            if (bugIndex > 4) {
//                bugIndex = 0;
//            }
//            String url = BUG_WEBHOOK.get(bugIndex);
//            String json = JSONUtil.toJsonStr(params);
//            String result = post(url, json);
//            bugIndex = bugIndex + 1;
//            log.info("企业微信Message sent, result: {}", result);
//        }
    }

    private static String post(String webhook, String json) {
        // 实现发送HTTP POST请求的逻辑，使用你喜欢的HTTP客户端库
        // 这里仅为示例，使用了hutool的HttpUtil来简化代码
        return cn.hutool.http.HttpUtil.createPost(webhook)
                .body(json)
                .execute()
                .body();
    }
}
